package com.java.digitalsignagesystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java.digitalsignagesystem.entity.Event;
import com.java.digitalsignagesystem.entity.Gjzs;
import com.java.digitalsignagesystem.entity.Result;
import com.java.digitalsignagesystem.service.EventService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Collections;
import java.util.List;

/**
 * (Event)表控制层
 *
 * @author makejava
 * @since 2024-02-29 15:51:41
 */
@RestController
@RequestMapping("event")
public class EventController {
    /**
     * 服务对象
     */
    @Resource
    private EventService eventService;


    /**
     * 通过唯一字段查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("/getByChooseIndex")
    public Result queryById(@RequestParam(value = "chooseIndex") String chooseIndex) throws ParseException {
        Event byChooseIndex = this.eventService.getByChooseIndex(chooseIndex);
        if (byChooseIndex != null) {
            return Result.error("数据已存在");
        }
        return Result.success();
    }

    /**
     * 新增数据
     *
     * @param event 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    public Result add(@RequestBody Event event) throws ParseException {
        Event byChooseIndex = eventService.getByChooseIndex(event.getChooseIndex());
        if (byChooseIndex != null) {
            return Result.success("数据已存在", byChooseIndex);
        }
        return Result.success(eventService.insert(event));
    }

    /**
     * 编辑数据
     *
     * @param event 实体
     * @return 编辑结果
     */
    @PutMapping("/update")
    public ResponseEntity<Event> edit(Event event) {
        return ResponseEntity.ok(this.eventService.update(event));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.eventService.deleteById(id));
    }


    @GetMapping("/ll")
    public Result getEvent(@RequestParam(value = "sourceDeptName") String sourceDeptName) {
        String gjzs = eventService.getGjzs(sourceDeptName);
        JSONArray jsonArray = JSONArray.parseArray(gjzs);
        System.out.println(jsonArray.toJSONString());
        List<Gjzs> list = JSONArray.parseArray(jsonArray.toJSONString(), Gjzs.class);
        System.out.println(list.toString());
        return Result.success(gjzs);
    }

    @GetMapping("/gjzs")
    public Result getEventToday(){
        List<String> chooseIndexList = eventService.getChooseIndexList();
        return Result.success(chooseIndexList);
    }

}

